﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.SqlClient;

namespace 数据函数库
{
    [Serializable]
    public class 实时数据Data
    {

        public const string IPField = "@IP";

        public const string GREENHOUSEADDRField = "@GreenhouseAddr";

        public const string NOTENUMField = "@NoteNum";

        public const string TIMEField = "@Time";

        public const string 空气温度1Field = "@空气温度1";

        public const string 空气温度2Field = "@空气温度2";

        public const string 空气温度3Field = "@空气温度3";

        public const string 空气湿度1Field = "@空气湿度1";

        public const string 空气湿度2Field = "@空气湿度2";

        public const string 空气湿度3Field = "@空气湿度3";

        public const string 光照1Field = "@光照1";

        public const string 光照2Field = "@光照2";

        public const string 光照3Field = "@光照3";

        public const string 土壤盐分1Field = "@土壤盐分1";

        public const string 土壤盐分2Field = "@土壤盐分2";

        public const string 土壤盐分3Field = "@土壤盐分3";

        public const string 土壤温度1Field = "@土壤温度1";

        public const string 土壤温度2Field = "@土壤温度2";

        public const string 土壤温度3Field = "@土壤温度3";

        public const string 土壤湿度1Field = "@土壤湿度1";

        public const string 土壤湿度2Field = "@土壤湿度2";

        public const string 土壤湿度3Field = "@土壤湿度3";

        public const string 二氧化碳1Field = "@二氧化碳1";

        public const string 二氧化碳2Field = "@二氧化碳2";

        public const string 二氧化碳3Field = "@二氧化碳3";

        private String _IP;

        private Int32 _GreenhouseAddr;

        private Int32 _NoteNum;

        private DateTime _Time;

        private Single _空气温度1;

        private Single _空气温度2;

        private Single _空气温度3;

        private Single _空气湿度1;

        private Single _空气湿度2;

        private Single _空气湿度3;

        private Single _光照1;

        private Single _光照2;

        private Single _光照3;

        private Single _土壤盐分1;

        private Single _土壤盐分2;

        private Single _土壤盐分3;

        private Single _土壤温度1;

        private Single _土壤温度2;

        private Single _土壤温度3;

        private Single _土壤湿度1;

        private Single _土壤湿度2;

        private Single _土壤湿度3;

        private Single _二氧化碳1;

        private Single _二氧化碳2;

        private Single _二氧化碳3;

        public String IP
        {
            get { return _IP; }
            set { _IP = value; }
        }
        public Int32 GreenhouseAddr
        {
            get { return _GreenhouseAddr; }
            set { _GreenhouseAddr = value; }
        }
        public Int32 NoteNum
        {
            get { return _NoteNum; }
            set { _NoteNum = value; }
        }
        public DateTime Time
        {
            get { return _Time; }
            set { _Time = value; }
        }
        public Single 空气温度1
        {
            get { return _空气温度1; }
            set { _空气温度1 = value; }
        }
        public Single 空气温度2
        {
            get { return _空气温度2; }
            set { _空气温度2 = value; }
        }
        public Single 空气温度3
        {
            get { return _空气温度3; }
            set { _空气温度3 = value; }
        }
        public Single 空气湿度1
        {
            get { return _空气湿度1; }
            set { _空气湿度1 = value; }
        }
        public Single 空气湿度2
        {
            get { return _空气湿度2; }
            set { _空气湿度2 = value; }
        }
        public Single 空气湿度3
        {
            get { return _空气湿度3; }
            set { _空气湿度3 = value; }
        }
        public Single 光照1
        {
            get { return _光照1; }
            set { _光照1 = value; }
        }
        public Single 光照2
        {
            get { return _光照2; }
            set { _光照2 = value; }
        }
        public Single 光照3
        {
            get { return _光照3; }
            set { _光照3 = value; }
        }
        public Single 土壤盐分1
        {
            get { return _土壤盐分1; }
            set { _土壤盐分1 = value; }
        }
        public Single 土壤盐分2
        {
            get { return _土壤盐分2; }
            set { _土壤盐分2 = value; }
        }
        public Single 土壤盐分3
        {
            get { return _土壤盐分3; }
            set { _土壤盐分3 = value; }
        }
        public Single 土壤温度1
        {
            get { return _土壤温度1; }
            set { _土壤温度1 = value; }
        }
        public Single 土壤温度2
        {
            get { return _土壤温度2; }
            set { _土壤温度2 = value; }
        }
        public Single 土壤温度3
        {
            get { return _土壤温度3; }
            set { _土壤温度3 = value; }
        }
        public Single 土壤湿度1
        {
            get { return _土壤湿度1; }
            set { _土壤湿度1 = value; }
        }
        public Single 土壤湿度2
        {
            get { return _土壤湿度2; }
            set { _土壤湿度2 = value; }
        }
        public Single 土壤湿度3
        {
            get { return _土壤湿度3; }
            set { _土壤湿度3 = value; }
        }
        public Single 二氧化碳1
        {
            get { return _二氧化碳1; }
            set { _二氧化碳1 = value; }
        }
        public Single 二氧化碳2
        {
            get { return _二氧化碳2; }
            set { _二氧化碳2 = value; }
        }
        public Single 二氧化碳3
        {
            get { return _二氧化碳3; }
            set { _二氧化碳3 = value; }
        }
        public 实时数据Data()
        {
            _IP = "";
            _GreenhouseAddr = 0;
            _NoteNum = 0;
            _Time = DateTime.Now;
            _空气温度1 = 0;
            _空气温度2 = 0;
            _空气温度3 = 0;
            _空气湿度1 = 0;
            _空气湿度2 = 0;
            _空气湿度3 = 0;
            _光照1 = 0;
            _光照2 = 0;
            _光照3 = 0;
            _土壤盐分1 = 0;
            _土壤盐分2 = 0;
            _土壤盐分3 = 0;
            _土壤温度1 = 0;
            _土壤温度2 = 0;
            _土壤温度3 = 0;
            _土壤湿度1 = 0;
            _土壤湿度2 = 0;
            _土壤湿度3 = 0;
            _二氧化碳1 = 0;
            _二氧化碳2 = 0;
            _二氧化碳3 = 0;
        }
    }

    public class 实时数据DataFactory
    {
        public static 实时数据Data Construct(IDataReader reader)
        {
            实时数据Data data = new 实时数据Data();

            try { data.IP = Convert.ToString(reader["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(reader["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(reader["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(reader["Time"]); }
            catch { }

            try { data.空气温度1 = Convert.ToSingle(reader["空气温度1"]); }
            catch { }

            try { data.空气温度2 = Convert.ToSingle(reader["空气温度2"]); }
            catch { }

            try { data.空气温度3 = Convert.ToSingle(reader["空气温度3"]); }
            catch { }

            try { data.空气湿度1 = Convert.ToSingle(reader["空气湿度1"]); }
            catch { }

            try { data.空气湿度2 = Convert.ToSingle(reader["空气湿度2"]); }
            catch { }

            try { data.空气湿度3 = Convert.ToSingle(reader["空气湿度3"]); }
            catch { }

            try { data.光照1 = Convert.ToSingle(reader["光照1"]); }
            catch { }

            try { data.光照2 = Convert.ToSingle(reader["光照2"]); }
            catch { }

            try { data.光照3 = Convert.ToSingle(reader["光照3"]); }
            catch { }

            try { data.土壤盐分1 = Convert.ToSingle(reader["土壤盐分1"]); }
            catch { }

            try { data.土壤盐分2 = Convert.ToSingle(reader["土壤盐分2"]); }
            catch { }

            try { data.土壤盐分3 = Convert.ToSingle(reader["土壤盐分3"]); }
            catch { }

            try { data.土壤温度1 = Convert.ToSingle(reader["土壤温度1"]); }
            catch { }

            try { data.土壤温度2 = Convert.ToSingle(reader["土壤温度2"]); }
            catch { }

            try { data.土壤温度3 = Convert.ToSingle(reader["土壤温度3"]); }
            catch { }

            try { data.土壤湿度1 = Convert.ToSingle(reader["土壤湿度1"]); }
            catch { }

            try { data.土壤湿度2 = Convert.ToSingle(reader["土壤湿度2"]); }
            catch { }

            try { data.土壤湿度3 = Convert.ToSingle(reader["土壤湿度3"]); }
            catch { }

            try { data.二氧化碳1 = Convert.ToSingle(reader["二氧化碳1"]); }
            catch { }

            try { data.二氧化碳2 = Convert.ToSingle(reader["二氧化碳2"]); }
            catch { }

            try { data.二氧化碳3 = Convert.ToSingle(reader["二氧化碳3"]); }
            catch { }

            return data;
        }
        public static 实时数据Data Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static 实时数据Data Construct(DataTable dt, int rowIndex)
        {
            实时数据Data data = new 实时数据Data();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度1 = Convert.ToSingle(dr["空气温度1"]); }
            catch { }

            try { data.空气温度2 = Convert.ToSingle(dr["空气温度2"]); }
            catch { }

            try { data.空气温度3 = Convert.ToSingle(dr["空气温度3"]); }
            catch { }

            try { data.空气湿度1 = Convert.ToSingle(dr["空气湿度1"]); }
            catch { }

            try { data.空气湿度2 = Convert.ToSingle(dr["空气湿度2"]); }
            catch { }

            try { data.空气湿度3 = Convert.ToSingle(dr["空气湿度3"]); }
            catch { }

            try { data.光照1 = Convert.ToSingle(dr["光照1"]); }
            catch { }

            try { data.光照2 = Convert.ToSingle(dr["光照2"]); }
            catch { }

            try { data.光照3 = Convert.ToSingle(dr["光照3"]); }
            catch { }

            try { data.土壤盐分1 = Convert.ToSingle(dr["土壤盐分1"]); }
            catch { }

            try { data.土壤盐分2 = Convert.ToSingle(dr["土壤盐分2"]); }
            catch { }

            try { data.土壤盐分3 = Convert.ToSingle(dr["土壤盐分3"]); }
            catch { }

            try { data.土壤温度1 = Convert.ToSingle(dr["土壤温度1"]); }
            catch { }

            try { data.土壤温度2 = Convert.ToSingle(dr["土壤温度2"]); }
            catch { }

            try { data.土壤温度3 = Convert.ToSingle(dr["土壤温度3"]); }
            catch { }

            try { data.土壤湿度1 = Convert.ToSingle(dr["土壤湿度1"]); }
            catch { }

            try { data.土壤湿度2 = Convert.ToSingle(dr["土壤湿度2"]); }
            catch { }

            try { data.土壤湿度3 = Convert.ToSingle(dr["土壤湿度3"]); }
            catch { }

            try { data.二氧化碳1 = Convert.ToSingle(dr["二氧化碳1"]); }
            catch { }

            try { data.二氧化碳2 = Convert.ToSingle(dr["二氧化碳2"]); }
            catch { }

            try { data.二氧化碳3 = Convert.ToSingle(dr["二氧化碳3"]); }
            catch { }

            return data;
        }
        public static 实时数据Data Construct(DataGridView dgv, int rowIndex)
        {
            实时数据Data data = new 实时数据Data();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度1 = Convert.ToSingle(dr["空气温度1"]); }
            catch { }

            try { data.空气温度2 = Convert.ToSingle(dr["空气温度2"]); }
            catch { }

            try { data.空气温度3 = Convert.ToSingle(dr["空气温度3"]); }
            catch { }

            try { data.空气湿度1 = Convert.ToSingle(dr["空气湿度1"]); }
            catch { }

            try { data.空气湿度2 = Convert.ToSingle(dr["空气湿度2"]); }
            catch { }

            try { data.空气湿度3 = Convert.ToSingle(dr["空气湿度3"]); }
            catch { }

            try { data.光照1 = Convert.ToSingle(dr["光照1"]); }
            catch { }

            try { data.光照2 = Convert.ToSingle(dr["光照2"]); }
            catch { }

            try { data.光照3 = Convert.ToSingle(dr["光照3"]); }
            catch { }

            try { data.土壤盐分1 = Convert.ToSingle(dr["土壤盐分1"]); }
            catch { }

            try { data.土壤盐分2 = Convert.ToSingle(dr["土壤盐分2"]); }
            catch { }

            try { data.土壤盐分3 = Convert.ToSingle(dr["土壤盐分3"]); }
            catch { }

            try { data.土壤温度1 = Convert.ToSingle(dr["土壤温度1"]); }
            catch { }

            try { data.土壤温度2 = Convert.ToSingle(dr["土壤温度2"]); }
            catch { }

            try { data.土壤温度3 = Convert.ToSingle(dr["土壤温度3"]); }
            catch { }

            try { data.土壤湿度1 = Convert.ToSingle(dr["土壤湿度1"]); }
            catch { }

            try { data.土壤湿度2 = Convert.ToSingle(dr["土壤湿度2"]); }
            catch { }

            try { data.土壤湿度3 = Convert.ToSingle(dr["土壤湿度3"]); }
            catch { }

            try { data.二氧化碳1 = Convert.ToSingle(dr["二氧化碳1"]); }
            catch { }

            try { data.二氧化碳2 = Convert.ToSingle(dr["二氧化碳2"]); }
            catch { }

            try { data.二氧化碳3 = Convert.ToSingle(dr["二氧化碳3"]); }
            catch { }

            return data;
        }
        public static 实时数据Data Construct(DataRow dr)
        {
            实时数据Data data = new 实时数据Data();

            try { data.IP = Convert.ToString(dr["IP"]); }
            catch { }

            try { data.GreenhouseAddr = Convert.ToInt32(dr["GreenhouseAddr"]); }
            catch { }

            try { data.NoteNum = Convert.ToInt32(dr["NoteNum"]); }
            catch { }

            try { data.Time = Convert.ToDateTime(dr["Time"]); }
            catch { }

            try { data.空气温度1 = Convert.ToSingle(dr["空气温度1"]); }
            catch { }

            try { data.空气温度2 = Convert.ToSingle(dr["空气温度2"]); }
            catch { }

            try { data.空气温度3 = Convert.ToSingle(dr["空气温度3"]); }
            catch { }

            try { data.空气湿度1 = Convert.ToSingle(dr["空气湿度1"]); }
            catch { }

            try { data.空气湿度2 = Convert.ToSingle(dr["空气湿度2"]); }
            catch { }

            try { data.空气湿度3 = Convert.ToSingle(dr["空气湿度3"]); }
            catch { }

            try { data.光照1 = Convert.ToSingle(dr["光照1"]); }
            catch { }

            try { data.光照2 = Convert.ToSingle(dr["光照2"]); }
            catch { }

            try { data.光照3 = Convert.ToSingle(dr["光照3"]); }
            catch { }

            try { data.土壤盐分1 = Convert.ToSingle(dr["土壤盐分1"]); }
            catch { }

            try { data.土壤盐分2 = Convert.ToSingle(dr["土壤盐分2"]); }
            catch { }

            try { data.土壤盐分3 = Convert.ToSingle(dr["土壤盐分3"]); }
            catch { }

            try { data.土壤温度1 = Convert.ToSingle(dr["土壤温度1"]); }
            catch { }

            try { data.土壤温度2 = Convert.ToSingle(dr["土壤温度2"]); }
            catch { }

            try { data.土壤温度3 = Convert.ToSingle(dr["土壤温度3"]); }
            catch { }

            try { data.土壤湿度1 = Convert.ToSingle(dr["土壤湿度1"]); }
            catch { }

            try { data.土壤湿度2 = Convert.ToSingle(dr["土壤湿度2"]); }
            catch { }

            try { data.土壤湿度3 = Convert.ToSingle(dr["土壤湿度3"]); }
            catch { }

            try { data.二氧化碳1 = Convert.ToSingle(dr["二氧化碳1"]); }
            catch { }

            try { data.二氧化碳2 = Convert.ToSingle(dr["二氧化碳2"]); }
            catch { }

            try { data.二氧化碳3 = Convert.ToSingle(dr["二氧化碳3"]); }
            catch { }

            return data;
        }
    }
    public class 实时数据DataDBOption
    {
        public static int Insert(实时数据Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into 实时数据 (");
            sql.Append("IP, ");
            sql.Append("GreenhouseAddr, ");
            sql.Append("NoteNum, ");
            sql.Append("Time, ");
            sql.Append("空气温度1, ");
            sql.Append("空气温度2, ");
            sql.Append("空气温度3, ");
            sql.Append("空气湿度1, ");
            sql.Append("空气湿度2, ");
            sql.Append("空气湿度3, ");
            sql.Append("光照1, ");
            sql.Append("光照2, ");
            sql.Append("光照3, ");
            sql.Append("土壤盐分1, ");
            sql.Append("土壤盐分2, ");
            sql.Append("土壤盐分3, ");
            sql.Append("土壤温度1, ");
            sql.Append("土壤温度2, ");
            sql.Append("土壤温度3, ");
            sql.Append("土壤湿度1, ");
            sql.Append("土壤湿度2, ");
            sql.Append("土壤湿度3, ");
            sql.Append("二氧化碳1, ");
            sql.Append("二氧化碳2, ");
            sql.Append("二氧化碳3");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.IP.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.GreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.NoteNum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Time.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气温度1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气温度2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气温度3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气湿度1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气湿度2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.空气湿度3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.光照1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.光照2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.光照3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤盐分1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤盐分2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤盐分3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤温度1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤温度2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤温度3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤湿度1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤湿度2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.土壤湿度3.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.二氧化碳1.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.二氧化碳2.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.二氧化碳3.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 实时数据 ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 实时数据 ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(实时数据Data d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from 实时数据 ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static int Change大棚信息(string ip, uint oldGreenhouseAddr, byte oldNoteNum, uint newGreenhouseAddr, byte newNoteNum)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE    实时数据 SET ");
            sql.Append("GreenhouseAddr =");
            sql.Append(newGreenhouseAddr.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append("NoteNum =");
            sql.Append(newNoteNum.ToString());
            sql.Append("");
            sql.Append(" ");   
            sql.Append("WHERE     (GreenhouseAddr =");
            sql.Append(oldGreenhouseAddr.ToString());
            sql.Append(") AND (NoteNum =");
            sql.Append(oldNoteNum.ToString());
            sql.Append(") AND (IP = '");
            sql.Append(ip);
            sql.Append("')");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
    }
}